package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;


import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.tj.jinzao.base.BaseActivity;

import com.tj.jinzao.R;
import com.tj.jinzao.callback.IRegisterLoginView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.RegisterLoginPresenter;
import com.tj.jinzao.utils.ToastUtils;

/**
 * 启动页
 * Created by wjy on 2018/5/3.
 */
public class SplashActivity extends BaseActivity<RegisterLoginPresenter> implements IRegisterLoginView {

    @Override
    protected RegisterLoginPresenter createPresenter() {
        return new RegisterLoginPresenter(this);
    }

    private Handler mHandler;

    @Override
    protected void setupView() {
        super.setupView();
        mHandler = new Handler();
        checkSkip();
    }

    public void checkSkip() {
        User user = UserManager.getInstance().getUser();
        Log.w("用户信息qqqqq",""+UserManager.getInstance().getUser().toString());
        if (user!=null&&user.getAuthentication() != 0) {
//            mPresenter.loginChat(user);
            loginSplashChat(user);
        } else {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                }
            }, 2500);
        }
    }
    //三次登录失败则验证账号密码
    private int posType=0;
    //网易云密码错误，登录好几次才可以登录成功，问题暂时未知，有时间待解决，此段代码最好不要加
    public void loginSplashChat(final User user) {
        Log.w("用户信息=====",""+user.getYunxinAccount());
        NimUIKit.login(new LoginInfo(String.valueOf(user.getYunxinAccount()), "123456"), new RequestCallback<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo param) {
                //如果性别未知，则完善资料
                if (UserManager.getInstance().getUser().getGender() == 3) {
                    if (TextUtils.isEmpty(user.getMobile())) {
                        Intent to=new Intent();
                        to.putExtra("typeLogin",0);
                        to.setClass(SplashActivity.this,LoginActivity.class);
                        startActivity(to);
                    } else {
                        startActivity(new Intent(SplashActivity.this, CompleteInfoActivity.class));
                    }
                    finish();
                } else {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }

            }

            @Override
            public void onFailed(int code) {
                Log.w("用户信息=====",""+code);
                posType++;
                if(posType>3){
                    ToastUtils.showToast(SplashActivity.this,"登录失效，请重新登录");
                    Intent to=new Intent();
                    to.putExtra("typeLogin",0);
                    to.setClass(SplashActivity.this,LoginActivity.class);
                    startActivity(to);
                    finish();
                }else {
                    loginSplashChat(user);
                }

            }

            @Override
            public void onException(Throwable exception) {
            }
        });
        updateUserInfo(user);
    }

    @Override
    protected void onStop() {
        super.onStop();
        posType=0;
    }

    @Override
    public int getLayoutResource() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        return R.layout.activity_splash;
    }

    @Override
    public void onGetCodeSuccess(String s) {

    }

    @Override
    public void onGetCodeFailed(String reason) {

    }

    @Override
    public void onCheckCodeSuccess(String token) {

    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(Object o) {
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }
}
